package com.lucun.es_transaction.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import java.util.Random;

@RestController
public class EmailController {
    @Autowired
    JavaMailSender mailSender;//注入QQ发送邮件的bean
    //定义发送的内容
//    public static String body = "成功";

    @RequestMapping("/email")
    public Object email(@RequestParam String qq, String title,String content) {
        /*
         * 短信验证码自生成
         * */
        int num = 0;
        Random random = new Random();
        StringBuffer stringBuffer1 = new StringBuffer();
        for (int i = 0; i < 7; i++) {
            num = random.nextInt(9);
            stringBuffer1.append(num);
        }
        content = stringBuffer1.toString();
        try {
            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom("690412667@qq.com");//设置发件qq邮箱
            qq += "@qq.com";                    //补全地址
//            qq += "@126.com";                    //补全地址
            message.setTo(qq);                //设置收件人
            message.setSubject(title);    //设置标题
            message.setText(content,true);    // 设置发送内容 第二个参数true表示使用HTML语言来编写邮件
//            FileSystemResource file = new FileSystemResource(
//            File file = new File("图片路径");
//            helper.addAttachment("图片.jpg", file);//添加带附件的邮件
//            helper.addInline("picture",file);//添加带静态资源的邮件
            this.mailSender.send(mimeMessage);
            return "发送成功";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "发送成功";
        }
    }

}
